import * as redis from 'redis';
import app from '../app'

function getRedisID(pID) {
    return "tower" + app.env + pID
}

class RedisHandle {
    client: any
    constructor() {

    }
    init = async (serverName?: string) => {
        return new Promise(resolve => {
            this.client = redis.createClient(6379, '127.0.0.1');
            this.client.on('connect', () => {
                console.log(serverName + ' redis 连接成功')
                resolve({})
            })
        })
    }

    expire = async (id, time) => {
        return new Promise(resolve => {
            this.client.expire(id, time, () => {
                resolve(null)
            })
        })
    }

    get = async (id): Promise<string> => {
        return new Promise(resolve => {
            this.client.get(id, (err, data) => {
                resolve(data)
            })
        })
    }

    getAll = async (id) => {
        return new Promise(resolve => {
            this.client.keys(id, (err, data) => {
                resolve(data)
            })
        })
    }

    increaseBy = async (id, num) => {
        return new Promise(resolve => {
            this.client.incrby(id, num, (err, data) => {
                resolve(data)
            })
        })
    }

    set = async (id, data: string) => {
        return new Promise(resolve => {
            this.client.set(id, data, () => {
                resolve(null)
            })
        })
    }

    del = async (id) => {
        return new Promise(resolve => {
            this.client.del(id, () => {
                resolve(null)
            })
        })
    }

    zadd = async (key, score, id) => {
        return new Promise(resolve => {
            this.client.zadd(key, score, id, (res) => {
                resolve(null)
            })
        })
    }

    zrange = async (key, min, max) => { //分数由低到高
        return new Promise(resolve => {
            this.client.zrange(key, min, max, (err, docs) => {
                resolve(docs)
            })
        })
    }

    zrevrange = async (key, min, max): Promise<any[]> => { //分数由高到低
        return new Promise(resolve => {
            this.client.zrevrange(key, min, max, (err, docs) => {
                resolve(docs)
            })
        })
    }

    zrangeWITHSCORES = async (key, min, max): Promise<any[]> => { //分数由低到高
        return new Promise(resolve => {
            this.client.zrange(key, min, max, 'WITHSCORES', (err, docs) => {
                resolve(docs)
            })
        })
    }

    zrevrangeWITHSCORES = async (key, min, max): Promise<any[]> => { //分数由高到低
        return new Promise(resolve => {
            this.client.zrevrange(key, min, max, 'WITHSCORES', (err, docs) => {
                resolve(docs)
            })
        })
    }

    zrangebyscoreWithAmount = async (key, min, max, amount): Promise<any[]> => { //分数由高到低
        return new Promise(resolve => {
            this.client.zrangebyscore(key, min, max, `LIMIT`, 0, amount, (err, docs) => {
                resolve(docs)
            })
        })
    }


    zrank = async (key, id) => { //分数由低到高
        return new Promise(resolve => {
            this.client.zrank(key, id, (err, doc) => {
                resolve(doc)
            })
        })
    }

    zrevrank = async (key, id): Promise<number> => { //分数由高到低
        return new Promise(resolve => {
            this.client.zrevrank(key, id, (err, doc) => {
                resolve(doc)
            })
        })
    }


    zscore = async (key, id): Promise<number> => {
        return new Promise(resolve => {
            this.client.zscore(key, id, (err, doc) => {
                resolve(doc)
            })
        })
    }

    zerm = async (key, id) => {
        return new Promise(resolve => {
            this.client.zrem(key, id, (err) => {
                resolve(null)
            })
        })
    }

    sadd = async (key, id) => {
        return new Promise(resolve => {
            this.client.sadd(key, id, () => {
                resolve(null)
            })
        })
    }
    smembers = async (key): Promise<any[]> => {
        return new Promise(resolve => {
            this.client.smembers(key, (err, data) => {
                resolve(data)
            })
        })
    }

    srem = async (key, id): Promise<any[]> => {
        return new Promise(resolve => {
            this.client.srem(key, id, () => {
                resolve(null)
            })
        })
    }
}

let redisHandle = new RedisHandle()
export default redisHandle 